﻿using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class GetMePanel : EventManager {
    public Button Shop,GY,Set,LiuYan,Share,Msg,HeadBtn;
    public GameObject HeadImage;
    public Text name, gold,Zhuanshi,ThisID;

	void Awake () {
        Shop.onClick.AddListener(ShowShop);
        GY.onClick.AddListener(ShowGy);
        Set.onClick.AddListener(ShowSet);
        LiuYan.onClick.AddListener(ShowLiuYan);
        Msg.onClick.AddListener(Send158);
        Share.onClick.AddListener(ShowShare);
        HeadBtn.onClick.AddListener(HeadBtnClick);
        SetText(name, GameData.GetInstance().userName);
	    SetText(gold, GameData.GetInstance().gold.ToString());
        SetText(Zhuanshi, GameData.GetInstance().Zhuan.ToString());
        SetText(ThisID, GameData.GetInstance().userID.ToString());
        BindingSource[0].GetComponent<Button>().onClick.AddListener(delegate
        {
            FaXianBtnClick();
        });
        BindingSource[1].GetComponent<Button>().onClick.AddListener(delegate
        {
            MyClub();
            MessageManager.GetInstance().SendMsg768();
            Des();
        });
        BindingSource[2].GetComponent<Button>().onClick.AddListener(delegate
        {
            FirstPanel();
            Des();
        });
        BindingSource[3].GetComponent<Button>().onClick.AddListener(delegate
        {
            ZhanJiBtnClick();
            //Des();
        });

	    Debug.Log(GameData.GetInstance().avatar);
        DownloadHeadTexture(HeadImage.GetComponent<RawImage>(), GameData.GetInstance().avatar);
    }

    protected void DownloadHeadTexture(RawImage texture, string url)            //下载头像图片
    {
        //texture = null;
#if WX_SDK
        if (url != null)
        {
            //用户头像，最后一个数值代表正方形头像大小（有0、46、64、96、132数值可选，0代表640*640正方形头像），用户没有头像时该项为空
            //url = url.Substring(0, url.Length - 1);
            //url += "132";    
            StartCoroutine(DownloadTexture(texture, url));
        }
        else
        {
            Debug.Log("下载头像Texture收到的url为空！");
        }
#else
        url = URLConst.DefaultUserHeadUrl;
        StartCoroutine(DownloadTexture(texture, url));
#endif
    }

    private IEnumerator DownloadTexture(RawImage texture, string url)
    {
        WWW www = new WWW(url);
        yield return www;
        texture.texture = www.texture;
    }


    void Des()
    {
        Destroy(this.gameObject);
    }
    void FaXianBtnClick()
    {
        MessageManager.GetInstance().Send7140();
    }
    /// <summary>
    /// 展示俱乐部面板
    /// </summary>
    void ClubBtnClick()
    {
        //UIManager.ChangeUI(UIManager.PageState.RulePage, delegate (GameObject target)
        //{
        //    target.GetComponent<GameHallShowRulePage>().InformationSetting();
        //});
    }
    void ZhanJiBtnClick()
    {
        MessageManager.GetInstance().Send7131(0, GameData.GetInstance().userID);
    }
    void ShowShop()
    {
        UIManager.ChangeUI(UIManager.PageState.Shop, (GameObject obj) =>
        {
            obj.GetComponent<ShopPanel>().InformationSetting();
        });
    }
    void ShowGy() {
        UIManager.ChangeUI(UIManager.PageState.Gy, (GameObject obj) =>
        {
            obj.GetComponent<GYPanel>().InformationSetting();
        });
    }
    void ShowSet()
    {
        UIManager.ChangeUI(UIManager.PageState.Set, (GameObject obj) =>
        {
            //obj.transform.GetChild(0).GetChild(1).GetChild(0).GetComponent<Button>().onClick.AddListener(delegate { Destroy(obj); });
            //obj.transform.GetChild(2).GetComponent<Button>().onClick.AddListener(delegate {
            //PlayerPrefs.DeleteKey(DefaultKeyConst.VisitorTokenKey);
            //Application.Quit();
       // });
        });
    }
    void ShowLiuYan() {
        UIManager.ChangeUI(UIManager.PageState.LiuYan, (GameObject obj) =>
        {
            obj.GetComponent<LiuYanPanel>().InformationSetting();
        });
    }
    void ShowShare() {
        UIManager.ChangeUI(UIManager.PageState.SharePage, (GameObject obj) =>
        {
            obj.GetComponent<SharePage>().InformationSetting();
        });
    }

    void HeadBtnClick()// 点击头像弹出用户的头像界面
    {
        GameObject setnumber = Instantiate(Resources.Load("Prefab/ChangeInfo/ChangeInfo")) as GameObject;
        setnumber.transform.SetParent(GameObject.Find("addNode").transform);
        setnumber.transform.localPosition = Vector3.zero;
        setnumber.transform.localScale = Vector3.one;
        Debug.Log("dakai zijide touxiang maingban ");
        setnumber.GetComponent<ChangeInfo>().SetAcc(GameData.GetInstance().userID);
    }

    void MyClub()
    {
        UIManager.ChangeUI(UIManager.PageState.Club, (GameObject obj) =>
        {
            obj.GetComponent<MyClubbPanel>().InformationSetting();
        });
    }
    void FirstPanel()
    {
        UIManager.ChangeUI(UIManager.PageState.GameHallPage, (GameObject obj) =>
        {
            obj.GetComponent<GameHallPanel>().InformationSetting();
        });
    }
    void Send158()
    {
        MessageManager.GetInstance().Send7158(int.Parse(GameData.GetInstance().userID));
        SaveDate.GetInstance().Dairu = false;
    }
    public override void InformationSetting()
    {
        if (SaveDate.GetInstance().Dairu)
        {
            BindingSource[6].SetActive(true);
        }
        else
        {
            BindingSource[6].SetActive(false);
        }
    }
}
